package com.gomo;

import java.io.IOException;

import org.json.JSONException;
import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.AndroidHttpTransport;
import org.xmlpull.v1.XmlPullParserException;

public class SoapService {

	public SoapObject parseWebService1(String soapAction, String url,
			SoapObject request) throws IOException, XmlPullParserException,
			JSONException {

		SoapObject response = null;
		try {
			SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
					SoapEnvelope.VER11);
			envelope.dotNet = true;

			// envelope.implicitTypes = true;
			envelope.encodingStyle = "utf-8";
			envelope.enc = SoapSerializationEnvelope.ENC2001;
			envelope.xsd = SoapEnvelope.XSD;
			envelope.xsi = SoapEnvelope.XSI;

			// envelope.headerOut = ;
			// this is an Element[] created before
			envelope.encodingStyle = SoapEnvelope.ENC;
			envelope.setAddAdornments(false);
			envelope.implicitTypes = false;

			envelope.setOutputSoapObject(request);
			AndroidHttpTransport androidHttpTransport = new AndroidHttpTransport(
					url);

			androidHttpTransport
					.setXmlVersionTag("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
			androidHttpTransport.call(soapAction, envelope);
			androidHttpTransport.debug = true;
			response = (SoapObject) envelope.getResponse();

		} catch (NumberFormatException e) {
			GlobalService.HandleException(e);
		} catch (IllegalArgumentException e) {
			GlobalService.HandleException(e);
		}

		return response;

	}

	public SoapObject parseWebService(String soapAction, String url,
			SoapObject request) {
		request.getNamespace();
		SoapObject response = new SoapObject(request.getNamespace(),
				request.getName());
		try {
			SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
					SoapEnvelope.VER11);
			envelope.dotNet = true;

			envelope.encodingStyle = "utf-8";
			envelope.enc = SoapSerializationEnvelope.ENC2001;
			envelope.xsd = SoapEnvelope.XSD;
			envelope.xsi = SoapEnvelope.XSI;

			envelope.encodingStyle = SoapEnvelope.ENC;
			envelope.setAddAdornments(false);
			envelope.implicitTypes = false;

			envelope.setOutputSoapObject(request);
			AndroidHttpTransport androidHttpTransport = new AndroidHttpTransport(url);

			androidHttpTransport
					.setXmlVersionTag("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");

			androidHttpTransport.call(soapAction, envelope);
			androidHttpTransport.debug = true;
			response = (SoapObject) envelope.getResponse();

		} catch (NumberFormatException e) {
			GlobalService.HandleException(e);
		} catch (IllegalArgumentException e) {
			GlobalService.HandleException(e);

		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (XmlPullParserException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return response;

	}

}
